#include "Crusher.h"
#include "Physics.h"
#include "Camera.h"
#include "Events\CSGD_EventSystem.h"
#include "Player.h"

Crusher::Crusher(void)
{
	GetStartingPos = false;
	CrushingDown = true;

	StartPos.fX = 0.0f;
	StartPos.fY = 0.0f;
}


Crusher::~Crusher(void)
{
}

void Crusher::Update(float deltaTime)
{
	//Get the starting pos 1 time
	if(GetStartingPos == false )
	{
		StartPos.fY = positionY;
		StartPos.fX = positionX;
		GetStartingPos = true;
	}

	//Have we reached our starting position?
	if(positionY - 1 <= StartPos.fY )
	{
		CrushingDown = true;
	}

	//Update hitbox
	Physics::UpdateColRect(hitBox, (int)positionX, (int)positionY);
	hitBox.top = (LONG)StartPos.fY-5;
	hitBox.bottom = (LONG)positionY+75;

	//Move the object
	CrushingDown ? this->positionY += 750 * deltaTime : this->positionY -= 50 * deltaTime;
}
void Crusher::Render(void) const
{
	int YSourcePos = 170 - (int)(positionY - StartPos.fY);
	RECT SourceRect = {0, YSourcePos, 64, 256};

	//Add the source rect
	//GameObject::Render();

	if(imageID != -1)
	{
		CSGD_TextureManager::GetInstance()->Draw(imageID,(int)(StartPos.fX - Camera::GetInstance()->OffsetX()),(int)(StartPos.fY - Camera::GetInstance()->OffsetY() - 5),1.0f,1.0f,&SourceRect);
	}
}
void Crusher::HandleCollision(GameObject& other, CollisionInfo& info)
{
	if( other.GetObjectID() != "Player" && other.GetObjectID() != "Metal Box" && other.GetObjectID() != "Wood Box" && other.GetObjectID() != "Radiation Barrel")
		this->CrushingDown = false;
	else
	{
		//only crush if crusher is currently going down
		if((CrushingDown && info.Side == CollisionSide::TOP)  /*|| !FloatEquals(other.GetIsCrushing(),0.0f)*/)
		{
			//if(FloatEquals(other.GetIsCrushing(), 0.0f))
			//{
			//	other.SetIsCrushing(0.9f);
			//	other.SetCrushMaxHeight(other.GetHitBox().bottom - other.GetHitBox().top);
			//}
			//if( /*other.GetIsCrushing() <= 0.7f*/)
			{
				if(other.GetObjectID() == "Player")
					CSGD_EventSystem::GetInstance()->SendEvent("KillPlayer", nullptr, nullptr, (void*)this);
				else
					other.SetIsActive(false);
			}
			//else
			//{
			//	RECT tempBox = other.GetHitBox();
			//	tempBox.top = this->GetHitBox().bottom;
			//	other.SetHitBox(tempBox);
			//	//other.SetIsCrushing(this->GetHitBox().bottom 
			//	float tempHeight =(float)(tempBox.bottom - tempBox.top);
			//	float tempOrig = (float)other.GetCrushMaxHeight();
			//	other.SetIsCrushing(tempHeight / tempOrig);
			//	other.SetPosY((float)tempBox.bottom);
			//}
		}
		else
		{
			if(info.Side == CollisionSide::BOTTOM)
				other.SetPosY( other.GetPosY() - (info.Overlap.bottom - info.Overlap.top));
			if(info.Side == CollisionSide::TOP)
				other.SetPosY( other.GetPosY() + (info.Overlap.bottom - info.Overlap.top));
			if(info.Side == CollisionSide::LEFT)
				other.SetPosX( other.GetPosX() + (info.Overlap.right - info.Overlap.left));
			if(info.Side == CollisionSide::RIGHT)
				other.SetPosX( other.GetPosX() - (info.Overlap.right - info.Overlap.left));
			other.SetHitBox(Physics::UpdateColRect(other.GetHitBox(), (int)(other.GetPosX()), (int)(other.GetPosY())));
		}
	}
}